#include <bits/stdc++.h>
using namespace std;
using ll = long long;

static const int mod = 1'000'000'007;  //'

static void testcase() {
  int n;
  cin >> n;
  vector<int> a(n), b(n), d(n);
  vector<int> pa(n);
  vector<bool> done(n);
  for (auto &ai : a) {
    cin >> ai;
    --ai;
  }
  for (auto &bi : b) {
    cin >> bi;
    --bi;
  }
  for (auto &di : d) {
    cin >> di;
    --di;
  }
  for (int i = 0; i < n; ++i) {
    pa[a[i]] = i;
  }
  ll res = 1;
  for (int i = 0; i < n; ++i) {
    if (done[i]) continue;
    if (d[i] == -1) continue;
    for (int j = i; !done[j]; j = pa[b[j]]) done[j] = true;
  }
  for (int i = 0; i < n; ++i) {
    if (done[i]) continue;
    if (d[i] == -1 && a[i] != b[i]) res = (2 * res) % mod;
    for (int j = i; !done[j]; j = pa[b[j]]) done[j] = true;
  }
  cout << res << '\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int TC;
  cin >> TC;
  for (int tc = 1; tc < TC + 1; ++tc) testcase();
}
